|
RAIS
3.2
|
00001 00028 using System; 00029 using System.Collections.Generic; 00030 using System.Data.SqlClient; 00031 using System.Text; 00032 00033 using RAIS.Common; 00034 00035 namespace RAIS.DataAccessLayer 00036 { 00040 public class SearchManagement 00041 { 00042 //********************************************************************* 00043 #region Public static methods 00044 00048 public static Dictionary<string, string> GetSearchItems() 00049 { 00050 Dictionary<string, string> searchItems = new Dictionary<string, string>(); 00051 00052 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00053 { 00054 sqlConnection.Open(); 00055 00056 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetSearchItems, sqlConnection)) 00057 { 00058 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00059 00060 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00061 { 00062 while (sqlReader.Read()) 00063 { 00064 searchItems.Add(sqlReader["Search Item"].ToString() 00065 , sqlReader["Search Item"].ToString()); 00066 } 00067 } 00068 } 00069 } 00070 return searchItems; 00071 } 00077 public static Dictionary<string, SearchDefinition> GetSearchDefinitionsBySearchItem(string searchItem) 00078 { 00079 Dictionary<string, SearchDefinition> searchDefinitions = new Dictionary<string, SearchDefinition>(); 00080 00081 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00082 { 00083 sqlConnection.Open(); 00084 00085 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetSearchDefinitionsBySearchItem, sqlConnection)) 00086 { 00087 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00088 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SearchItem, searchItem); 00089 00090 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00091 { 00092 while (sqlReader.Read()) 00093 { 00094 SearchDefinition searchDefinition = new SearchDefinition(sqlReader); 00095 searchDefinitions.Add(searchDefinition.SearchField, searchDefinition); 00096 } 00097 } 00098 } 00099 } 00100 return searchDefinitions; 00101 } 00108 public static bool SearchInDB(Search search, ref string resultMessage) 00109 { 00110 bool success = true; 00111 00112 Dictionary<int, string> searchResults = new Dictionary<int, string>(); 00113 00114 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00115 { 00116 sqlConnection.Open(); 00117 00118 try 00119 { 00120 using (SqlCommand sqlCommand = new SqlCommand(search.SearchDefinition.SearchSQL, sqlConnection)) 00121 { 00122 sqlCommand.CommandType = System.Data.CommandType.Text; 00123 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SearchText, search.SearchText); 00124 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_UserID, search.UserID); 00125 00126 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00127 { 00128 while (sqlReader.Read()) 00129 { 00130 if (sqlReader.FieldCount < 2 00131 || sqlReader[0].GetType().Name != "Int32") 00132 { 00133 resultMessage = Common.Messages.WRONG_SEARCH_SELECT; 00134 success = false; 00135 } 00136 else 00137 { 00138 searchResults.Add((int)sqlReader[0], sqlReader[1].ToString()); 00139 } 00140 } 00141 } 00142 } 00143 search.SearchResult = searchResults; 00144 } 00145 catch (Exception e) 00146 { 00147 resultMessage = e.Message + Constants.DATA_BASE_ERROR_MASSAGE; 00148 success = false; 00149 } 00150 } 00151 return success; 00152 } 00153 00165 public static void AddSearchItem(string searchItem, string searchField, string menu, string mask, int dynamicMask, string searchSQL) 00166 { 00167 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00168 { 00169 sqlConnection.Open(); 00170 00171 using (SqlCommand sqlCommand = new SqlCommand("usp_AddSearchItem", sqlConnection)) 00172 { 00173 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00174 DataAccessUtilities.AddParameter(sqlCommand, "@SearchItem", searchItem); 00175 DataAccessUtilities.AddParameter(sqlCommand, "@SearchField", searchField); 00176 DataAccessUtilities.AddParameter(sqlCommand, "@Menu", menu); 00177 DataAccessUtilities.AddParameter(sqlCommand, "@Mask", mask); 00178 DataAccessUtilities.AddParameter(sqlCommand, "@DynamicMask", dynamicMask); 00179 DataAccessUtilities.AddParameter(sqlCommand, "@SearchSQL", searchSQL); 00180 00181 sqlCommand.ExecuteNonQuery(); 00182 00183 } 00184 } 00185 } 00186 00187 public static void DeleteSearchItem(int itemID) 00188 { 00189 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00190 { 00191 sqlConnection.Open(); 00192 00193 using (SqlCommand sqlCommand = new SqlCommand( 00194 string.Format("DELETE FROM [System - Search] WHERE PK_SQL_ID = {0}", itemID) 00195 , sqlConnection)) 00196 { 00197 sqlCommand.CommandType = System.Data.CommandType.Text; 00198 sqlCommand.ExecuteNonQuery(); 00199 } 00200 } 00201 } 00202 #endregion 00203 } 00204 }